package org.usmile.algorithms.huawei.acm;

import java.util.*;

// https://fcqian.blog.csdn.net/article/details/128273764
public class _优秀学员统计 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[] dayCount = new int[30];
        for (int i = 0; i < 30; i++) {
            dayCount[i] = sc.nextInt();
        }

        int[][] dayIds = new int[30][];
        for (int i = 0; i < 30; i++) {
            int m = dayCount[i];
            dayIds[i] = new int[m];
            for (int j = 0; j < m; j++) {
                dayIds[i][j] = sc.nextInt();
            }
        }

        System.out.println(getResult(dayIds));
    }

    public static String getResult(int[][] dayIds) {
        Map<Integer, List<Integer>> employees = new HashMap<>();
        for (int i = 0; i < dayIds.length; i++) {
            int[] ids = dayIds[i];
            for (int id : ids) {
                if (!employees.containsKey(id)) {
                    employees.put(id, new ArrayList<>());
                    employees.get(id).add(id);
                }
                employees.get(id).add(i);
            }
        }

        List<List<Integer>> list = new ArrayList<>(employees.values());
        list.sort((o1, o2) ->
                o1.size() == o2.size() ?
                        (o1.get(1).equals(o2.get(1)) ?
                                o1.get(0) - o2.get(0)
                                : o1.get(1) - o2.get(1))
                        : o2.size() - o1.size());


        StringJoiner sj = new StringJoiner(" ");
        // 不考虑并列的情况，按规则返回前5名员工的id即可，如果当月打卡的员工少于5个，按规则排序返回所有有打卡记录的员工id
        for (int i = 0; i < Math.min(5, list.size()); i++) {
            sj.add(list.get(i).get(0) + "");
        }
        return sj.toString();
    }
}
